﻿using Engine;
using Game;
using GameEntitySystem;
using TemplatesDatabase;

#region ComponentShop
public class ComponentShop : ComponentInventoryBase, IUpdateable, IInventory
{
    public int SoldSlotIndex => SlotsCount - 1;
    public int SoldSlotIndex2 => SlotsCount - 2;
    public int SoldSlotIndex3 => SlotsCount - 3;
    public SubsystemNK m_subsystemHL;
    public ComponentPlayer componentPlayer;
    public SubsystemTime subsystemTime;
    public NK9 nk;

    public int m_activeSlotIndex;

    public override int ActiveSlotIndex
    {
        get
        {
            return m_activeSlotIndex;
        }
        set
        {
            m_activeSlotIndex = MathUtils.Clamp(value, 0, SlotsCount - 3);
        }
    }



    public override int GetSlotCapacity(int slotIndex, int value)
    {
        if (slotIndex == SoldSlotIndex)
        {
            if (value == 570)
            {
                return base.GetSlotCapacity(slotIndex, value);
            }
            return 0;
        }
        if (slotIndex == SoldSlotIndex2)
        {
            if (value != null)
            {
                return base.GetSlotCapacity(slotIndex, value);
            }
            return 0;
        }
        if (slotIndex == SoldSlotIndex3)
        {
            if (value != null)
            {
                return base.GetSlotCapacity(slotIndex, value);
            }
            return 0;
        }

        return base.GetSlotCapacity(slotIndex, value);
    }



    UpdateOrder IUpdateable.UpdateOrder => ((IUpdateable)componentPlayer).UpdateOrder;
    public void Update(float dt)
    {

        if (GetSlotValue(SoldSlotIndex) == 570)
        {
            m_subsystemHL.Money = GetSlotCount(SoldSlotIndex);
        }
        else
        {
            m_subsystemHL.Money = 0;
        }

        if (GetSlotCount(SoldSlotIndex) != m_subsystemHL.Money)
        {


        }

        if (GetSlotValue(SoldSlotIndex2) != 0)
        {
            m_subsystemHL.Money = 0;
        }

        if (GetSlotValue(SoldSlotIndex3) != null)
        {
            nk.ID = GetSlotValue(SoldSlotIndex3);
        }

        if (GetSlotValue(SoldSlotIndex3) == 587)
        {
            if (subsystemTime.PeriodicGameTimeEvent(240.0, 0))
            {
                RemoveSlotItems(SoldSlotIndex3, 1);
            }
        }
        if (GetSlotValue(SoldSlotIndex3) == 588)
        {
            if (subsystemTime.PeriodicGameTimeEvent(240.0, 0))
            {
                RemoveSlotItems(SoldSlotIndex3, 1);
            }
        }
        if (GetSlotValue(SoldSlotIndex3) == 589)
        {
            if (subsystemTime.PeriodicGameTimeEvent(240.0, 0))
            {
                RemoveSlotItems(SoldSlotIndex3, 1);
            }
        }
        if (GetSlotValue(SoldSlotIndex3) == 590 && componentPlayer.ComponentHealth.Health <= 0f)
        {
            RemoveSlotItems(SoldSlotIndex3, 1);

        }
        if (GetSlotValue(SoldSlotIndex3) == 592)
        {
            if (subsystemTime.PeriodicGameTimeEvent(240.0, 0))
            {
                RemoveSlotItems(SoldSlotIndex3, 1);
            }
        }
        if (GetSlotValue(SoldSlotIndex3) == 595)
        {
            if (subsystemTime.PeriodicGameTimeEvent(240.0, 0))
            {
                RemoveSlotItems(SoldSlotIndex3, 1);
            }
        }
        if (GetSlotValue(SoldSlotIndex3) == 596)
        {
            if (subsystemTime.PeriodicGameTimeEvent(240.0, 0))
            {
                RemoveSlotItems(SoldSlotIndex3, 1);
            }
        }




    }

    public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
    {
        m_subsystemHL = Project.FindSubsystem<SubsystemNK>(throwOnError: true);
        componentPlayer = Entity.FindComponent<ComponentPlayer>(true);
        subsystemTime = Project.FindSubsystem<SubsystemTime>();
        nk = Entity.FindComponent<NK9>(true);
        ActiveSlotIndex = valuesDictionary.GetValue<int>("ActiveSlotIndex");
        base.Load(valuesDictionary, idToEntityMap);
    }

    public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
    {
        base.Save(valuesDictionary, entityToIdMap);
        valuesDictionary.SetValue("ActiveSlotIndex", ActiveSlotIndex);
    }



}
#endregion ComponentShop